/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast.view;

import org.chaoticengine.ast.GlobalData;
import org.chaoticengine.ast.model.AirShip;
import org.chaoticengine.ast.model.Location;
import org.chaoticengine.ast.pattern.Model;
import org.chaoticengine.ast.pattern.ViewController;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Detailed view of the currently selected object.
 * 
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class SelectionView extends ViewController<GlobalData> {
    protected GlobalData gd = null;
    protected LocationDetailsView locView = null;
    protected AirShipDetailsView shipView = null;
    protected ViewController activeView = null;
    
    public SelectionView() {
        super();

        locView = new LocationDetailsView();
        locView.setPosition(new Vector2f());
        locView.setBoundingRectangle(new Rectangle(0, 0, 200, 400));

        shipView = new AirShipDetailsView();
        shipView.setPosition(new Vector2f());
        shipView.setBoundingRectangle(new Rectangle(0, 0, 200, 400));
    }

    @Override
    public GlobalData getModel() {
        return(gd);
    }

    @Override
    public void keyPressed(int key, char c) {}

    @Override
    public void keyReleased(int key, char c) {}

    @Override
    public void onMouseClick(int x, int y, int button) {}

    @Override
    public void onMouseEnter() {}

    @Override
    public void onMouseExit() {}

    @Override
    public void onMouseMove(int oldx, int oldy, int newx, int newy) {}

    @Override
    public void onMouseRelease(int x, int y, int button) {}

    @Override
    public void onMouseWheelDown() {}

    @Override
    public void onMouseWheelUp() {}

    @Override
    public void refresh(GlobalData model) {
        gd = model;

        ViewController<? extends Model> tv = gd.getSelected();

        /** Disassociate old views. */
        if (locView.getModel() != null) {
            locView.getModel().removeObsever(locView);
        }

        if (shipView.getModel() != null) {
            shipView.getModel().removeObsever(shipView);
        }

        activeView = null;

        if (tv != null) {
            /** Associate new views. */
            Model mdl = tv.getModel();

            if (mdl instanceof Location) {
                Location l = (Location) mdl;

                l.addObserver(locView);
                activeView = locView;
            } else if (mdl instanceof AirShip) {
                AirShip as = (AirShip) mdl;

                as.addObserver(shipView);
                activeView = shipView;
            }
        }
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        grphcs.translate(position.x, position.y);

        if (activeView != null) {
            activeView.render(gc, sbg, grphcs);
        }

        grphcs.translate(-position.x, -position.y);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) {}

    @Override
    public void onDeselect() {
    }

    @Override
    public void onSelect() {
    }


}
